package com.cloudcc.cloudframe.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectivityUtils {

	/**
	 * 判断是否有网络
	 * 
	 * @return
	 */
	public static boolean hasInternet(Context context) {
		boolean flag;
		if (((ConnectivityManager) context.getSystemService("connectivity"))
				.getActiveNetworkInfo() != null)
			flag = true;
		else
			flag = false;
		return flag;
	}

	/**
	 * 没有网络返回 true
	 * 
	 * @param context
	 * @return
	 */
	public static boolean hasNoInternet(Context context) {
		return !hasInternet(context);
	}

	/**
	 * 判断当前网络环境是否wifi网络
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isWifiOpen(Context context) {
		boolean isWifiConnect = false;
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// check the networkInfos numbers
		NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
		for (int i = 0; i < networkInfos.length; i++) {
			if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
				if (networkInfos[i].getType() == ConnectivityManager.TYPE_MOBILE) {
					isWifiConnect = false;
				}
				if (networkInfos[i].getType() == ConnectivityManager.TYPE_WIFI) {
					isWifiConnect = true;
				}
			}
		}
		return isWifiConnect;
	}
}
